;;; packages - package definitions for GNU Guix
;;; Copyright (C) 2017 ng0
;;;
;;; This program is free software: you can redistribute it and/or modify
;;; it under the terms of the GNU Affero General Public License as
;;; published by the Free Software Foundation, either version 3 of the
;;; License, or (at your option) any later version.
;;;
;;; This program is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU Affero General Public License for more details.
;;;
;;; You should have received a copy of the GNU Affero General Public License
;;; along with this program.  If not, see <http://www.gnu.org/licenses/>.

(define-module (ng0 packages shells)
  #:use-module (guix packages)
  #:use-module ((guix licenses) #:prefix license:)
  #:use-module (guix git-download)
  #:use-module (guix download)
  #:use-module (guix build-system gnu)
  #:use-module (gnu packages)
  #:use-module (gnu packages libbsd)
  #:use-module (gnu packages pkg-config))
  ;;#:use-module (gnu packages musl))

(define-public linenoise
  (package
    (name "linenoise")
    (version "1.0")
    (source
      (origin
        (method url-fetch)
        (uri (string-append "https://github.com/antirez/linenoise/"
                            "archive/" version ".tar.gz"))
        (file-name (string-append name "-" version ".tar.gz"))
        (sha256
          (base32
            "05006hd56xcvxjdpll4x720bpfan7vwqmxbw8a2kvm10w57ll1gm"))))
    (build-system gnu-build-system)
    (arguments
      `(#:tests? #f
        #:make-flags (list "CC=gcc")
        #:phases
        (modify-phases %standard-phases
          (delete 'configure)
          (replace 'install
           (lambda* (#:key outputs #:allow-other-keys)
             (let* ((out (assoc-ref outputs "out")))
               (install-file "linenoise.h" (string-append out "/include/linenoise"))
               (install-file "linenoise.c" (string-append out "/include/linenoise"))
               #t))))))
    (home-page "https://github.com/antirez/linenoise")
    (synopsis "minimal zero-config readline replacement")
    (description
      "Linenoise is a minimal, zero-config, readline replacement.
Its features include:

@enumerate
@item Single and multi line editing mode with the usual key bindings implemented.
@item History handling.
@item Completion.
@item Hints (suggestions at the right of the prompt as you type).
@item About 1,100 lines of BSD license source code.
@item Only uses a subset of VT100 escapes (ANSI.SYS compatible).
@end enumerate\n")
    (license license:bsd-4)))

(define-public s
  (let ((commit "6604341edb3a775ff94415762af3ee9bd86bfb3c")
        (revision "1"))
  (package
    (name "s")
    (version (string-append "0.0.0-" revision "." (string-take commit 7)))
    (source
      (origin
        (method git-fetch)
        (uri (git-reference
               (url "https://github.com/rain-1/s")
               (commit commit)))
        (file-name (string-append name "-" version "-checkout"))
        (sha256
          (base32
            "1075cml6dl15d770j3m12yz90cjacsdslbv3gank1nxd76vmpdcr"))))
    (build-system gnu-build-system)
    (inputs
      `(("linenoise" ,linenoise)))
    (arguments
      `(#:tests? #f
        #:make-flags (list "CC=gcc")
        #:phases
        (modify-phases %standard-phases
         (add-after 'unpack 'install-directory-fix
           (lambda* (#:key outputs #:allow-other-keys)
             (let* ((out (assoc-ref outputs "out"))
                    (bin (string-append out "/bin")))
             (substitute* "Makefile"
               (("out") bin))
             #t)))
         (add-after 'install 'manpage
           (lambda* (#:key outputs #:allow-other-keys)
             (install-file "s.1" (string-append (assoc-ref outputs "out")
                                                "/share/man/man1"))))
         (replace 'configure
           (lambda* (#:key inputs outputs #:allow-other-keys)
             ;; At this point linenoise is meant to be included,
             ;; so we have to really copy it into the working directory
             ;; of s.
             (let* ((linenoise (assoc-ref inputs "linenoise"))
                    (noisepath (string-append linenoise "/include/linenoise"))
                    (out (assoc-ref outputs "out")))
               ;;(mkdir-p "linenoise")
               ;;(with-directory-excursion "linenoise"
               ;;  (copy-file-recursively noisepath "."))
               (copy-recursively noisepath "linenoise")
               (substitute* "s.c"
                 (("/bin/s") (string-append out "/bin/s")))
               #t))))))
    (home-page "https://github.com/rain-1/s")
    (synopsis "Extremely minimal shell with the simplest syntax possible")
    (description
      "@code{s} is a new shell that aims to be extremely simple, and
implemened it in a short number of lines of code (right now it's 780 lines
plus 1200 for the linenoise library).  Technically it is a
\"command interpreter\" not a shell since shell implies it implements the
POSIX shell standard.

What's missing: We don't have globs or \"splatting\" where a variable $FOO
turns into multiple command line arguments.  One token stays one token forever.
This is a \"no surprises\" straightforward approach.

We don't even have redirection operators > in the shell language.  They are
added as extra programs. So you write grep foo bar | > out.txt.  > is just
another unix command.  < is essentially cat.  A glob program is also provided
along with s, so you write glob rm *.")
    (license license:bsd-3))))

(define-public loksh
  (package
    (name "loksh")
    (version "6.1")
    (source
      (origin
        (method url-fetch)
        (uri (string-append "https://github.com/dimkr/loksh/archive/"
                            version ".tar.gz"))
        (file-name (string-append name "-" version ".tar.gz"))
        (sha256
         (base32
          "1wg7ds56yr8fgg1m149bi53bvrwccwiashmwknggza1sqgj9m2lq"))))
    (build-system gnu-build-system)
    (inputs
      `(("libbsd" ,libbsd)
        ("pkg-config" ,pkg-config)))
    (arguments
      `(#:tests? #f ;No tests included
        #:make-flags (list "CC=gcc" "HAVE_LIBBSD=1"
                           (string-append "DESTDIR="
                                          (assoc-ref %outputs "out"))
                           "PREFIX=")
        #:phases
        (modify-phases %standard-phases
          (delete 'configure)))) ;No configure script
    (home-page "https://github.com/dimkr/loksh")
    (synopsis "Linux port of OpenBSD ksh")
    (description
     "loksh is a Linux port of OpenBSD's ksh targeting musl.
It is a small, interactive shell targeted at resource-constrained systems.")
    ;; Stated in LEGAL. Check before sending to Guix.
    (license license:public-domain)))
